//
//  NoneArtistView.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/9.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "NoneArtistView.h"
#import "NoneArtistCell.h"
#import "IGMyFocusMemberApi.h"
#import "IGArtGalleryAttentApi.h"

@interface NoneArtistView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSString *uid;
    NSString *pageNum;//页数，从1开始
}
@property (nonatomic, strong) UIView *layerView;
@property (nonatomic, strong) UIView *nonView;
@property (nonatomic, strong) UICollectionView *hotRecView;
@property (nonatomic, assign) NSInteger section;
@property (nonatomic, strong)IGMyFocusMemberApi *myMemberApi;
@property (nonatomic, strong) IGArtGalleryAttentApi *attentApi;
@property (nonatomic, strong)NSMutableArray *recDataArr;
@end
@implementation NoneArtistView
#pragma mark - 懒加载
-(NSMutableArray *)recDataArr{
    if (!_recDataArr) {
        _recDataArr=[NSMutableArray array];
    }
    return _recDataArr;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
        [self setConstraint];
    }
    return self;
}
-(void)firstPage{
    pageNum= @"1";
    uid=[IGUser getUid];
    [self requestData];
}
-(void)requestData{
    self.myMemberApi=[[IGMyFocusMemberApi alloc]initWithUid:uid pageNum:pageNum];
    @KWeakObj(self)
    [self.myMemberApi startWithDecodeClass:[IGMyFocusMemberResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGMyFocusMemberResp *resp=responseObject;
            IGMyFocusMemberModel *model=resp.data;
            if ([pageNum isEqualToString:@"1"])
            {
                _recDataArr = [NSMutableArray arrayWithArray:model.recMemberList];
            }
            else
            {
                [_recDataArr removeAllObjects];
                [_recDataArr addObjectsFromArray:model.recMemberList];
            }
            [selfWeak.hotRecView reloadData];
        }
    }];
}
- (void)createUI
{
    [self addSubview:self.layerView];
    [self.layerView addSubview:self.nonView];
    [self.layerView addSubview:self.hotRecView];
    [self firstPage];
}

- (void)setConstraint
{
    [self.nonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(120);
    }];

    [self.hotRecView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(_nonView.mas_bottom).offset(5);
        make.height.mas_equalTo(380);
    }];
}

#pragma mark - 懒加载
- (UIView *)layerView
{
    if (!_layerView) {
        _layerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - NavigationMagin-44)];
    }
    return _layerView;
}
-(UIView *)nonView{
    if (!_nonView) {
        _nonView = [[UIView alloc]initWithFrame:CGRectZero];
        _nonView.backgroundColor=[UIColor whiteColor];
        UILabel *noLabel=[[UILabel alloc]init];
        noLabel.frame=CGRectMake(0, 32, KSCREENWIDTH,16);
        noLabel.text=@"暂无关注";
        noLabel.font=[UIFont font16];
        noLabel.textColor=[UIColor c4Color];
        [noLabel sizeToFit];
        noLabel.x=KSCREENWIDTH/2-noLabel.width/2;
        [_nonView addSubview:noLabel];
        
        UILabel *noLabel1=[[UILabel alloc]init];
        noLabel1.frame=CGRectMake(0, 32, KSCREENWIDTH,16);
        noLabel1.text=@"关注你喜欢的艺术家到这里";
        noLabel1.textColor=[UIColor c5Color];
        noLabel1.font=[UIFont font14];
        noLabel1.top=noLabel.bottom+17;
        [noLabel1 sizeToFit];
        noLabel1.x=KSCREENWIDTH/2-noLabel1.width/2;
        [_nonView addSubview:noLabel1];
    }
    return _nonView;
}
- (UICollectionView *)hotRecView{
    
    if (_hotRecView ==nil) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        _hotRecView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, _nonView.bottom+5,KSCREENWIDTH,370)collectionViewLayout:layout];
        _hotRecView.backgroundColor=[UIColor whiteColor];
        _hotRecView.delegate=self;
        _hotRecView.dataSource=self;
        _hotRecView.translatesAutoresizingMaskIntoConstraints=NO;
       _hotRecView.alwaysBounceVertical=YES;
        [_hotRecView registerNib:[UINib nibWithNibName:@"NoneArtistCell" bundle:nil] forCellWithReuseIdentifier:@"NoneArtistCell"];
        [_hotRecView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"GCollectionViewheader"];
    }
    return _hotRecView;
}

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.recDataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NoneArtistCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"NoneArtistCell" forIndexPath:indexPath];
    cell.data=self.recDataArr[indexPath.item];
    [cell followOwerAction:^(MyFocusMemberModel *info) {
        self.attentApi = [[IGArtGalleryAttentApi alloc]init];
        _attentApi.operatorType = [info.attentionState boolValue]?@"0":@"1";
        _attentApi.museumId = info.aid;
        [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            if (responseObject) {
                
            } else {
                if ([info.attentionState boolValue]) {
                    [self presentMessageTips:NSLocalizedString(@"Follow Failed", nil)];
                } else {
                    [self presentMessageTips:NSLocalizedString(@"Cancel Failed", nil)];
                }
            }
        }];
    }];
    [cell setNeedsLayout];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {//这是头部视图
            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GCollectionViewheader" forIndexPath:indexPath];
            header.backgroundColor=[UIColor mainBackgroundColor];
            UIView *headerView = [[UIView alloc]init];
            headerView.backgroundColor = [UIColor whiteColor];
            headerView.frame =CGRectMake(0, 0, KSCREENWIDTH, 50);
            UILabel *hot=[[UILabel alloc]initWithFrame:CGRectZero];
            hot.text=@"热门艺术家推荐";
            hot.font=[UIFont font16];
            hot.textColor=[UIColor c4Color];
            [hot sizeToFit];
            hot.x=KSCREENWIDTH/2-hot.width/2;
            hot.centerY=headerView.centerY;
            [headerView addSubview:hot];
            UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            changeBtn.frame=CGRectMake(0, 0, 50, 30);
            [changeBtn setTitle:@"换一批" forState:UIControlStateNormal];
            [changeBtn setTitle:@"换一批" forState:UIControlStateSelected];
            [changeBtn setImage:[UIImage imageNamed:@"gexchange"] forState:UIControlStateNormal];
            [changeBtn setImage:[UIImage imageNamed:@"gexchange"] forState:UIControlStateSelected];
            [changeBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
            [changeBtn setTitleColor:[UIColor c5Color] forState:UIControlStateNormal];
            [changeBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
            [changeBtn sizeToFit];
            changeBtn.centerY=headerView.centerY;
            changeBtn.right=headerView.right-12;
            [changeBtn addTarget:self action:@selector(changeBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:changeBtn];
            [header addSubview:headerView];
            return header;
        }
    }
    return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KSCREENWIDTH - 50 - 35) / 3, (KSCREENWIDTH - 50 - 35) / 3 + 40);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 30, 0 ,0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
//设置header尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return CGSizeMake(KSCREENWIDTH, 51);
    }
    return CGSizeZero;
}
-(void)changeBtnClick{
    pageNum = [NSString stringWithFormat:@"%ld", [pageNum integerValue] + 1];
    [self requestData];
}
@end
